/**
 *
 * @param {title, startTime, endTime, location, description} options
 */
export default function useCalendar(options) {
  uni.getSetting({
    success: (res) => {
      if (!res.authSetting["scope.addPhoneCalendar"]) {
        uni.authorize({
          scope: "scope.addPhoneCalendar",
          success() {
            addCalendar(options);
          },
        });
      } else {
        addCalendar(options);
      }
    },
  });
}

function addCalendar(options) {
  uni.addPhoneCalendar({
    ...options,
    success() {
      uni.showToast({
        title: "添加成功",
        icon: "success",
      });
    },
    fail(error) {
      console.log(error);
      uni.showToast({
        title: error.message ?? "添加失败",
        icon: "error",
      });
    },
  });
}
